import time
import logging

#from Case.python_ui.Common.get_path import GetPath
from Case.python_ui.Common.get_path import GetPath


class MyLogging(object):
    def get_logging(self, msg, level, name):
        my_log = logging.getLogger(name)  # Get log collector
        my_log.setLevel("DEBUG")  # Set the level of the log collector to debug or higher
        # Defines the log output format
        formatter = logging.Formatter('%(asctime)s-'

                                      '%(pathname)s-'
                                      '%(filename)s-'
                                      '[line:%(lineno)d]-'
                                      '%(name)s-'
                                      'Log information：%(message)s')
        # Define log output channels
        cs = logging.StreamHandler()  # OutPut to console
        print(GetPath.log_path)
        # file_path = GetPath.log_path + "\\{}" \
        #                                ".log".format(time.strftime("%Y-%m-%d", time.localtime()))

        file_path = GetPath.log_path + "\\{}.log".format(time.strftime("%Y-%m-%d", time.localtime()))
        print(file_path)
        fs = logging.FileHandler(file_path, mode='a+', encoding="UTF-8")  # OutPut to file
        # Set the bug level of the output channel
        cs.setLevel("DEBUG")
        # formatting
        cs.setFormatter(formatter)
        fs.setFormatter(formatter)
        # Bind the output channel to the log collector
        my_log.addHandler(cs)
        my_log.addHandler(fs)
        if level == "DEBUG":
            my_log.debug(msg)
        elif level == "WARNING":
            my_log.warning(msg)
        elif level == "ERROR":
            my_log.error(msg)
        elif level == "INFO":
            my_log.error(msg)
        else:
            my_log.critical(msg)

        # Remove the log collector after each collection. Otherwise, logs will be printed repeatedly
        my_log.removeHandler(cs)
        my_log.removeHandler(fs)
        # Disable the log output channel
        cs.close()
        fs.close()


if __name__ == "__main__":
    name = "test"
    MyLogging().get_logging("out put error information{}".format(name), "INFO", "test")
